package com.cskaoyan.service.market;

import com.cskaoyan.bean.market.Region;
import com.cskaoyan.bean.market.RegionExample;
import com.cskaoyan.mapper.RegionMapper;
import com.cskaoyan.vo.market.RegionDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: mall
 * @description:
 * @author: Mr.Mengmeng
 * @create: 2021-04-09 09:04
 **/

// 行政区域
@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    RegionMapper regionMapper;

    // 行政区域 admin/region/list
    @Override
    public RegionDataVo regionList() {

        RegionDataVo regionDataVo = new RegionDataVo();

        // 获取全部省级别的数据
        RegionExample province = new RegionExample();
        province.setDistinct(true);
        province.createCriteria().andTypeEqualTo(Byte.valueOf("1"));
        List<Region> provinceList = regionMapper.selectByExample(province);
        // 获取市级别的数据
        RegionExample city = new RegionExample();
        city.setDistinct(true);
        city.createCriteria().andTypeEqualTo(Byte.valueOf("2"));
        List<Region> cityList = regionMapper.selectByExample(city);
        // 获取区级数据
        RegionExample town = new RegionExample();
        town.setDistinct(true);
        town.createCriteria().andTypeEqualTo(Byte.valueOf("3"));
        List<Region> townList = regionMapper.selectByExample(town);


        ArrayList<RegionDataVo.DataBean> dataBeans = new ArrayList<>();
        for (Region region : provinceList) {
            // 封装省级数据
            RegionDataVo.DataBean dataBean = new RegionDataVo.DataBean(
                    region.getId(), region.getName(), region.getType(), region.getCode(), null);

            ArrayList<RegionDataVo.DataBean.ChildrenBeanX> citys = new ArrayList<>();
            for (Region region1 : cityList) {

                if (region1.getPid() .equals( region.getId())) {
                    // 封装市级数据
                    RegionDataVo.DataBean.ChildrenBeanX childrenBeanX = new RegionDataVo.DataBean.ChildrenBeanX(
                            region1.getId(), region1.getName(), region1.getType(), region1.getCode(), null);

                    ArrayList<RegionDataVo.DataBean.ChildrenBeanX.ChildrenBean> towns = new ArrayList<>();
                    for (Region region2 : townList) {

                        if (region2.getPid() .equals(region1.getId())) {
                            // 封装区级数据
                            RegionDataVo.DataBean.ChildrenBeanX.ChildrenBean childrenBean = new RegionDataVo.DataBean.ChildrenBeanX.ChildrenBean(
                                    region2.getId(), region2.getName(), region2.getType(), region2.getCode());
                            towns.add(childrenBean);
                        }

                    }
                    childrenBeanX.setChildren(towns);

                    citys.add(childrenBeanX);
                }
            }
            dataBean.setChildren(citys);

            dataBeans.add(dataBean);
        }

        regionDataVo.setData(dataBeans);
        return regionDataVo;
    }
}
